import Vue from 'vue'

Vue.filter('myFixed', function (value, fixed) {
  // 返回处理后的值
  if (value !== 0 && !value) {
    return ''
  }
  return parseFloat(value).toFixed(fixed || 2)
})

Vue.filter('urlFilter', function (value) {
  // 返回处理后的值
  if (!value || value.length < 1) {
    return
  }
  return value[0].url
})

Vue.filter('timeFilter', function (value, fixed) {
  if (!value) {
    return
  }
  // 返回处理后的值
  let date = new Date().getTime()
  let diff = (date - value) / 1000
  
  if (diff <= 60) {
    return '1分钟'
  } else if (diff > 60 && diff <= 60 * 60) {
    return Math.floor(diff / 60 ) + '分钟'
  } else if (diff > 60 * 60 && diff <= 24 * 60 * 60) {
    return Math.floor(diff / ( 60 * 60 )) + '小时'
  } else {
    return Math.floor(diff / ( 24 * 60 * 60 )) + '天'
  }
})

Vue.filter('ymdhmFilter', function (value, fixed) {
  // 返回处理后的值
  if (!value) {
    return ''
  }

  let reDay = function (val) {
    if (val < 10) {
      return '0' + val
    }

    return val
  }
  let date = new Date(value)
  let month = reDay(date.getMonth() + 1)
  let day = reDay(date.getDate())
  let hour = reDay(date.getHours())
  let minute = reDay(date.getMinutes())
  return month + '-' + day + ' ' + hour + ':' + minute
})

Vue.filter('fullTime', function (value) {
  // 返回处理后的值
  if (!value) {
    return ''
  }

  let reDay = function (val) {
    if (val < 10) {
      return '0' + val
    }

    return val
  }
  let date = new Date(value)
  let year = date.getFullYear()
  let month = reDay(date.getMonth() + 1)
  let day = reDay(date.getDate())
  let hour = reDay(date.getHours())
  let minute = reDay(date.getMinutes())
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute
})

Vue.filter('kindsFilter', function(value) {
  if (!value || value.length < 1) {
    return ''
  }

  return value[0].name
})

Vue.filter('number', function(value){
  if (!value) {
    return
  } else {
    var reg = /^\d{1,3}$/
    if (!reg.test(value)) {
      return 1
    }
  }
})

Vue.filter('lengthFilter', function(value){
  if (!value) {
    return
  } else {
    if (value.length <= 26) {
      return value
    } else {
      return value.substr(0, 26) + '...'
    }
  }
})